package com.gel.aiagent.demo.invoke;

import cn.hutool.core.map.MapUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class HttpAiInvoke {

    /**
     * 阿里云灵积模型DashScope API客户端
     */
    public static void main(String[] args) {
         String API_URL = "https://dashscope.aliyuncs.com/api/v1/services/aigc/text-generation/generation";
        /**
         * 发送文本生成请求
         *
         * @param model 模型名称，如"qwen-plus"
         * @param userContent 用户消息内容
         * @param systemContent 系统消息内容，可为null
         * @return 响应结果JSON字符串
         */
            // 构建消息列表
            List<Map<String, String>> messages = new ArrayList<>();

            // 添加系统消息（如果有）
            messages.add(MapUtil.builder(new HashMap<String, String>())
                    .put("role", "system")
                    .put("content", "You are a helpful assistant.")
                    .build());


            // 添加用户消息
            messages.add(MapUtil.builder(new HashMap<String, String>())
                    .put("role", "user")
                    .put("content", "你是谁")
                    .build());

            // 构建请求体
            Map<String, Object> requestBody = new HashMap<>();
            requestBody.put("model", "qwen-plus");

            Map<String, Object> input = new HashMap<>();
            input.put("messages", messages);
            requestBody.put("input", input);

            Map<String, Object> parameters = new HashMap<>();
            parameters.put("result_format", "message");
            requestBody.put("parameters", parameters);

            // 发送请求
            HttpResponse response = HttpRequest.post(API_URL)
                    .header("Authorization", "Bearer " + ApiKey.API_KEY)
                    .header("Content-Type", "application/json")
                    .body(JSONUtil.toJsonStr(requestBody))
                    .execute();

        System.out.println(response.body());
        }





}
